
var jsonAccessType={
  "status": 200,
  "totalFlow": 0,
  "userCapPackClassify": [{
    "id": 24581,
    "srcAddr": "10.16.195.164",
    "destAddr": "118.112.22.184",
    "srcPort": 58075,
    "destPort": 443,
    "protcolType": "6",
    "accessTypeId": 3,
    "packSize": 378,
    "date": "Dec 3, 2019 7:18:39 PM"
  }, {
    "id": 24582,
    "srcAddr": "118.112.22.184",
    "destAddr": "10.16.195.164",
    "srcPort": 80,
    "destPort": 58189,
    "protcolType": "6",
    "accessTypeId": 3,
    "packSize": 60,
    "date": "Dec 3, 2019 7:18:39 PM"
  }, {
    "id": 24583,
    "srcAddr": "10.16.195.164",
    "destAddr": "110.185.115.204",
    "srcPort": -1,
    "destPort": -1,
    "protcolType": "1",
    "accessTypeId": 2,
    "packSize": 296,
    "date": "Dec 3, 2019 7:18:42 PM"
  }, {
    "id": 24584,
    "srcAddr": "110.185.115.204",
    "destAddr": "10.16.195.164",
    "srcPort": -1,
    "destPort": -1,
    "protcolType": "1",
    "accessTypeId": 2,
    "packSize": 296,
    "date": "Dec 3, 2019 7:18:42 PM"
  }, {
    "id": 24585,
    "srcAddr": "10.16.195.164",
    "destAddr": "110.185.115.204",
    "srcPort": -1,
    "destPort": -1,
    "protcolType": "1",
    "accessTypeId": 2,
    "packSize": 148,
    "date": "Dec 3, 2019 7:19:08 PM"
  }, {
    "id": 24586,
    "srcAddr": "110.185.115.204",
    "destAddr": "10.16.195.164",
    "srcPort": -1,
    "destPort": -1,
    "protcolType": "1",
    "accessTypeId": 2,
    "packSize": 148,
    "date": "Dec 3, 2019 7:19:08 PM"
  }, {
    "id": 24587,
    "srcAddr": "10.16.195.164",
    "destAddr": "110.185.115.204",
    "srcPort": -1,
    "destPort": -1,
    "protcolType": "1",
    "accessTypeId": 2,
    "packSize": 148,
    "date": "Dec 3, 2019 7:19:10 PM"
  }, {
    "id": 24588,
    "srcAddr": "110.185.115.204",
    "destAddr": "10.16.195.164",
    "srcPort": -1,
    "destPort": -1,
    "protcolType": "1",
    "accessTypeId": 2,
    "packSize": 148,
    "date": "Dec 3, 2019 7:19:10 PM"
  }, {
    "id": 24589,
    "srcAddr": "10.16.195.164",
    "destAddr": "110.185.115.204",
    "srcPort": 58125,
    "destPort": 443,
    "protcolType": "6",
    "accessTypeId": 2,
    "packSize": 486,
    "date": "Dec 3, 2019 7:19:29 PM"
  }, {
    "id": 24590,
    "srcAddr": "118.112.22.184",
    "destAddr": "10.16.195.164",
    "srcPort": 80,
    "destPort": 58189,
    "protcolType": "6",
    "accessTypeId": 3,
    "packSize": 540,
    "date": "Dec 3, 2019 7:19:29 PM"
  }, {
    "id": 24591,
    "srcAddr": "10.16.195.164",
    "destAddr": "110.185.115.204",
    "srcPort": 58238,
    "destPort": 443,
    "protcolType": "6",
    "accessTypeId": 2,
    "packSize": 1445,
    "date": "Dec 3, 2019 7:19:31 PM"
  }, {
    "id": 24592,
    "srcAddr": "110.185.115.204",
    "destAddr": "10.16.195.164",
    "srcPort": 443,
    "destPort": 58238,
    "protcolType": "6",
    "accessTypeId": 2,
    "packSize": 10275,
    "date": "Dec 3, 2019 7:19:31 PM"
  }, {
    "id": 24593,
    "srcAddr": "10.16.195.164",
    "destAddr": "211.97.167.31",
    "srcPort": 1863,
    "destPort": 7469,
    "protcolType": "17",
    "accessTypeId": 18,
    "packSize": 130,
    "date": "Dec 3, 2019 7:21:48 PM"
  }, {
    "id": 24594,
    "srcAddr": "211.97.167.31",
    "destAddr": "10.16.195.164",
    "srcPort": 7469,
    "destPort": 1863,
    "protcolType": "17",
    "accessTypeId": 18,
    "packSize": 15580,
    "date": "Dec 3, 2019 7:21:49 PM"
  }, {
    "id": 24595,
    "srcAddr": "10.16.195.164",
    "destAddr": "211.97.167.31",
    "srcPort": 1863,
    "destPort": 7469,
    "protcolType": "17",
    "accessTypeId": 18,
    "packSize": 383,
    "date": "Dec 3, 2019 7:21:49 PM"
  }, {
    "id": 24596,
    "srcAddr": "10.16.195.164",
    "destAddr": "211.97.167.31",
    "srcPort": 1863,
    "destPort": 7469,
    "protcolType": "17",
    "accessTypeId": 18,
    "packSize": 383,
    "date": "Dec 3, 2019 7:21:55 PM"
  }
  ]
};
export default class FlowChartDataHandle {

  static getIpNum(ipAddress) {/*获取IP数*/

    let ip = ipAddress.split(".");

    let a = parseInt(ip[0]);

    let b = parseInt(ip[1]);

    let c = parseInt(ip[2]);

    let d = parseInt(ip[3]);

    var ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;

    return ipNum;

  }


  static isInner(userIp, begin, end) {

    return (userIp >= begin) && (userIp <= end);

  }

  static IsInnerIP(ipAddress) {

    let isInnerIp = false;//默认给定IP不是内网IP
    let ipNum = this.getIpNum(ipAddress);

    /**

     * 私有IP：A类  10.0.0.0    -10.255.255.255

     *       B类  172.16.0.0  -172.31.255.255

     *       C类  192.168.0.0 -192.168.255.255

     *       D类   127.0.0.0   -127.255.255.255(环回地址)

     **/

    let aBegin = this.getIpNum("10.0.0.0");

    let aEnd = this.getIpNum("10.255.255.255");

    let bBegin = this.getIpNum("172.16.0.0");

    let bEnd = this.getIpNum("172.31.255.255");

    let cBegin = this.getIpNum("192.168.0.0");

    let cEnd = this.getIpNum("192.168.255.255");

    let dBegin = this.getIpNum("127.0.0.0");

    let dEnd = this.getIpNum("127.255.255.255");

    isInnerIp = this.isInner(ipNum, aBegin, aEnd) || this.isInner(ipNum, bBegin, bEnd) || this.isInner(ipNum, cBegin, cEnd) || this.isInner(ipNum, dBegin, dEnd);

    //console.log('是否是内网:'+isInnerIp);

    return isInnerIp;
  }

//找大类型与小类型
  static findType(typeJson, typeId) {
    let resType = [];
    //console.log(typeJson);
   // console.log(typeId);
    for (let i = 0; i < typeJson.accessType.length; i++) {
      //console.log(typeId);
      //console.log(typeJson.accessType[i].id);
      if (typeJson.accessType[i].id === typeId) {
        resType.push(typeJson.accessType[i].typeName);
        resType.push(typeJson.accessType[i].siteName);
      }
    }
    //console.log(resType)
    return resType;
  }
  static sortData(dataList) {
    //console.log(dataList)
    for (let i = 0; i < dataList.length; i++) {
      let dataItem = dataList[i];
      for (var k in dataItem) {
        if ( k === 'data') {
           for(let j=0;j<dataItem[k].length;j++){
             if(dataItem[k][j]===undefined){
               dataItem[k][j]=0;
             }
           }
        }
      }
    }
    return null;
  }
//在数据列表中找到指定名称类型的数据变量
  static findDataItem(dataList, typeName, flowDir) {
    let flag = false;
    let flag1 = false;
    //console.log(dataList)
    for (let i = 0; i < dataList.length; i++) {
      flag = false;
      flag1 = false;
      let dataItem = dataList[i];

      for (var k in dataItem) {
        // console.log(k)
        // console.log(dataItem[k])
        if (k === 'name' && dataItem[k] === typeName) {
          flag = true;
        }
        // 判断是否是上下行流量
        if (k === 'stack' && dataItem[k] === flowDir) {
          flag1 = true;
        }

        if (flag && flag1 && k === 'data') {
          return dataItem[k];
        }
      }
    }
    return null;
  }
  static daysBetween(DateOne,DateTwo){
    let OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));
    let OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);
    let OneYear = DateOne.substring(0,DateOne.indexOf ('-'));
    let TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
    let TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);
    let TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));
    let cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);

    return Math.abs(cha);
  }
  static dateTran(myDate){
    let dateString;
    let temp =myDate;
    dateString=temp.getFullYear();
    dateString+="-";
    dateString+=temp.getMonth()+1;
    dateString+="-";
    dateString+=temp.getDate();

    return dateString;
  }
  static getDateInter(date0,date1){
    if(date0>date1) {
      return  Math.floor(((date0 -
        date1) / 60 / 60 / 24 / 1000));
    }else{
     return  Math.floor(((date1 -
        date0) / 60 / 60 / 24 / 1000));
    }
  }

  static saveAccessTypeList(accessTypeList,accessTypeName){
    for(let i=0;i<accessTypeList.length;i++){
      if(accessTypeList[i]===accessTypeName){
        return ;
      }
    }
    accessTypeList.push(accessTypeName)
  }

  //找到表中的协议类型
  static findPcoItem(dataPcoList,poctolName){
    let flag=false;
    for(let i=0;i<dataPcoList.length;i++){
      let tempItem=dataPcoList[i];
      flag=false;
      for (var key in tempItem) {
        var value=tempItem[key];
        if(key==='name'&&value===poctolName){
          flag=true;
        }else if(key==='value'&&flag){
          return tempItem;
        }
      }
    }
    return null;
  }
  static tran(jsonVal,typeJson,startDate,showType){
    let resDataList=[];
    let capPack=jsonVal.userCapPackClassify;

    //////线段图
    let accessType;
    //数据列表
    let dataList=[];
    ///////

    /////协议饼图////////////////////
    //数据列表
    let dataPcoList=[];
    let poctolList=[];
    /////////////////////////////////////

    ////折线图的数据处理////////////////////////////////
    //数据列表
    let accessTypeList=[];
    let dataLineList=[];
    let dataLineItemHead=['product'];
    //let dataItem=[];
    dataLineList.push(dataLineItemHead);
    //默认显示大类
    if(showType===undefined){
      showType=0;
    }
    ///////////////////////////////////////////////////



    //数据项
    /*let dataItem={
    'name':'',
    'type':'bar',
    'statck':'',
    'data':[]
    };
    */
  //console.log(startDate);
    if (capPack.length === 0){
      //线段图
      resDataList.push(dataList);
      //曲线图
      resDataList.push(dataLineList);
      //线段图类型列表
      resDataList.push(accessTypeList);

      //协议饼图
      resDataList.push(dataPcoList);
      //协议饼图类型列表
      resDataList.push(poctolList);
      return resDataList
    }
    for(let i=0;i<capPack.length ;i++){
      //////////////////////////////////线段图/////////////////////////////////////////////
      let dataItemFind;
      //找到对应的类型名称W
      accessType=this.findType(typeJson,capPack[i].accessTypeId);
      // 保存访问类型
      this.saveAccessTypeList(accessTypeList,accessType[0]);

      //console.log(accessType)
      //是否内网,或者是否上传
      let isInner=this.IsInnerIP(capPack[i].srcAddr);
      //找到指定类型的数据变量
      dataItemFind=this.findDataItem(dataList,accessType[0],isInner?'上行流量':'下行流量');

      if(dataItemFind === null){
        dataItemFind=[];
        dataItemFind.push(capPack[i].packSize);
        dataList.push({
          'name':accessType[0],
          'type':'bar',
          'stack':isInner?'上行流量':'下行流量',
          'data':dataItemFind
        });
      }else{
        let index=this.getDateInter(new Date(capPack[i].date),startDate);

        if(dataItemFind[index]!=null){
          dataItemFind[index]+=capPack[i].packSize;
        }else{
         // console.log(index);
          dataItemFind[index]=capPack[i].packSize;
        }
      }
      /////////////////////////////////////////////////////////////////////////////////////////////

      /////////////////////////////////////折线图//////////////////////////////////////////////////
      let dataItemFindLineChart=[];

      //找到对应的类型名称W
      //accessType=this.findType(typeJson,capPack[i].accessTypeId);

      dataItemFindLineChart=this.findLineChartDataItem(dataLineList,accessType[showType]);

      if(dataItemFindLineChart===null){
        dataItemFindLineChart=[];
        //如果没有，则还要在前面插入零
        dataItemFindLineChart.push(accessType[showType]);
        for(let j=0;j<i+1;j++){
          dataItemFindLineChart.push(0);
        }

        dataLineList.push(dataItemFindLineChart);
      }else{
        dataItemFindLineChart.push(capPack[i].packSize);
      }

      this.addOtherItem(dataLineList,dataItemFindLineChart);
      //capPack[i].date
      dataLineItemHead.push(capPack[i].date);

      ////////////////////////////////////////////////////////////////////////////////////////////

      ///////////////////////////////////协议饼图//////////////////////////////////////
      //let dataItemFind;
      dataItemFind=this.findPcoItem(dataPcoList,capPack[i].protcolType);

      if(dataItemFind === null){
        dataItemFind={
          name:capPack[i].protcolType,
          value:capPack[i].packSize
        };
        dataPcoList.push(dataItemFind);
      }else{
        dataItemFind.value+=capPack[i].packSize;
      }

    }

    for(let i=0;i<dataPcoList.length;i++){
      poctolList.push(dataPcoList[i].name);
    }

    this.sortData(dataList);
    //线段图
    resDataList.push(dataList);
    //曲线图
    resDataList.push(dataLineList);
    //线段图类型列表
    resDataList.push(accessTypeList);

    //协议饼图
    resDataList.push(dataPcoList);
    //协议饼图类型列表
    resDataList.push(poctolList);

    return resDataList;

  }


  //除指定项其它项都增加数据0
  static addOtherItem(dataList,noAddItem){
    for(let i=1;i<dataList.length ;i++){
      if(dataList[i]!==noAddItem){
        dataList[i].push(0);
      }
    }
  }
  //找到需要的曲线图数据项
  static findLineChartDataItem(dataList,TypeName){
    for(let i=0;i<dataList.length ;i++){
      if(dataList[i][0]!=null){
        if(dataList[i][0]===TypeName){
          return dataList[i];
        }
      }
    }
    return null;
  }

  static  lineChart(jsonVal,typeJson,showType){

    let capPack=jsonVal.userCapPackClassify;
    let accessType;

    //数据列表
    let dataList=[];
    let dataItemHead=['product'];
    //let dataItem=[];
    dataList.push(dataItemHead);

    for(let i=0;i<capPack.length ;i++){

      let dataItemFind=[];

      //找到对应的类型名称W
      accessType=this.findType(typeJson,capPack[i].accessTypeId);

      dataItemFind=this.findLineChartDataItem(dataList,accessType[showType]);

      if(dataItemFind===null){
        dataItemFind=[];
        //如果没有，则还要在前面插入零
        dataItemFind.push(accessType[showType]);
        for(let j=0;j<i+1;j++){
          dataItemFind.push(0);
        }

        dataList.push(dataItemFind);
      }else{
        dataItemFind.push(capPack[i].packSize);
      }

      this.addOtherItem(dataList,dataItemFind);
      //capPack[i].date
      dataItemHead.push(capPack[i].date);
    }

    return dataList;
  }
  static saveAlarmType(alarmTypelist,saveTypeName){
    for(let i=0;i<alarmTypelist.length;i++){
      if(alarmTypelist[i]===saveTypeName){
        return ;
      }
    }
    alarmTypelist.push(saveTypeName);
  }
  static alarmTran(alarmJson,startDate){
    let dayCount=0;
    let resDataList=[];//返回的数据
    let dayStrList=[];//显示的天
    let alarmTypeNameList=[];//报警的类型
    let listAlarmDate=[];//存放数据

    let alarmData=alarmJson.userAlarmList;

    resDataList.push(listAlarmDate);
    resDataList.push(alarmTypeNameList);
    resDataList.push(dayStrList);


    for(let i=0;i<alarmData.length;i++){
      let alarmDataRes=[];
      this.saveAlarmType(alarmTypeNameList,alarmData[i].alarmType);
      alarmDataRes=this.findDataItem(listAlarmDate,alarmData[i].alarmType,'a');
      if(alarmDataRes==null){
        alarmDataRes=[];
        alarmDataRes.push(1);
        listAlarmDate.push({
          'name':alarmData[i].alarmType,
          'type':'bar',
          'stack':'a',
          'coordinateSystem': 'polar',
          'data':alarmDataRes
        });
      }else{
        let index=this.getDateInter(startDate,new Date(alarmData[i].date));
        //console.log(index);
        if(alarmDataRes[index]!==undefined){
          alarmDataRes[index]+=1;
        }else{
          alarmDataRes[index]=1;
        }
        if(index>dayCount){
          dayCount=index;
        }
      }

    }
    for(let i=0;i<dayCount+1;i++){
      dayStrList.push("第"+(i+1)+"天");
    }
    this.sortData(listAlarmDate);

    return resDataList;
  }

  static typeJson={
    "status": 200,
    "totalFlow": 0,
    "accessType": [{
      "id": 1,
      "typeName": "音乐",
      "ip": "118.112.11.101",
      "port": 443,
      "siteName": "QQ音乐"
    }, {
      "id": 2,
      "typeName": "音乐",
      "ip": "110.185.115.204",
      "port": 443,
      "siteName": "QQ音乐"
    }, {
      "id": 3,
      "typeName": "音乐",
      "ip": "118.112.22.184",
      "port": 443,
      "siteName": "QQ音乐"
    }, {
      "id": 12,
      "typeName": "政治",
      "ip": "110.185.125.140",
      "port": 443,
      "siteName": "学习强国"
    }, {
      "id": 13,
      "typeName": "黑名单",
      "ip": "125.66.191.151",
      "port": 443,
      "siteName": "4399小游戏"
    }, {
      "id": 14,
      "typeName": "黑名单",
      "ip": "125.66.191.151",
      "port": 80,
      "siteName": "4399小游戏"
    }, {
      "id": 15,
      "typeName": "其它",
      "ip": "0.0.0.0",
      "port": -1,
      "siteName": "其它"
    }, {
      "id": 18,
      "typeName": "视频",
      "ip": "211.97.167.31",
      "port": -1,
      "siteName": "腾讯视频"
    }, {
      "id": 21,
      "typeName": "视频",
      "ip": "122.136.198.26",
      "port": -1,
      "siteName": "腾讯视频"
    }, {
      "id": 22,
      "typeName": "视频",
      "ip": "123.120.66.157",
      "port": -1,
      "siteName": "腾讯视频"
    }, {
      "id": 23,
      "typeName": "政治",
      "ip": "110.185.125.187",
      "port": 443,
      "siteName": "学习强国"
    }, {
      "id": 24,
      "typeName": "政治",
      "ip": "110.185.125.141",
      "port": 443,
      "siteName": "学习强国"
    }, {
      "id": 25,
      "typeName": "音乐",
      "ip": "101.72.201.35",
      "port": 443,
      "siteName": "网易云音乐"
    }, {
      "id": 26,
      "typeName": "视频",
      "ip": "223.221.180.20",
      "port": 443,
      "siteName": "腾讯视频"
    }, {
      "id": 27,
      "typeName": "视频",
      "ip": "101.206.186.14",
      "port": 443,
      "siteName": "腾讯视频"
    }, {
      "id": 28,
      "typeName": "视频",
      "ip": "36.250.239.37",
      "port": 443,
      "siteName": "爱奇艺"
    }, {
      "id": 29,
      "typeName": "视频",
      "ip": "1.82.215.22",
      "port": 443,
      "siteName": "腾讯视频"
    }, {
      "id": 30,
      "typeName": "视频",
      "ip": "14.215.178.14",
      "port": 443,
      "siteName": "爱奇艺"
    }, {
      "id": 31,
      "typeName": "视频",
      "ip": "112.80.255.84",
      "port": 443,
      "siteName": "爱奇艺"
    }, {
      "id": 32,
      "typeName": "视频",
      "ip": "180.97.93.50",
      "port": 443,
      "siteName": "爱奇艺"
    },{
      "id": 33,
      "typeName": "黑名单",
      "ip": "180.97.93.50",
      "port": 443,
      "siteName": "4399"

    }]
  }
}






// console.log(jsonAccessType);
// console.log(typeJson);
// tran(jsonAccessType,typeJson);



